#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QMessageBox"
#include "equ_include/Belord_PTC_9000.h"
#include "QDebug"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    canset=new CANset();
    Interface_Rtimer=new QTimer(this);
    Timer_10s=new QTimer(this);
    FaultListModel= new QStringListModel(this);
    Rest_count=0;
    StartTime=QTime::currentTime();
    FaultListModel->setStringList(FaultList);
    ui->listView_fault_list->setModel(FaultListModel);
    Interface_Init();
    Equ_runinfo_init();
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Data_Refresh()));
    connect(Interface_Rtimer,SIGNAL(timeout()),this,SLOT(Interface_Refresh()));
    connect(Timer_10s,SIGNAL(timeout()),this,SLOT(Auto_Runlog_info()));
    connect(this,SIGNAL(Displayrefresh_signal(int)),this,SLOT(Fault_show()));
    connect(this,SIGNAL(Displayrefresh_signal(int)),this,SLOT(EQRun_loglist()));
    //connect(this,SIGNAL(Displayrefresh_signal(int)),mqttset,SLOT(mqtt_sed()));
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::Interface_Init()      //界面配置初始化
{
    Interface_rt=200;
    Timer_10s_rt=10000;//log记录时间
    Interface_Rtimer->stop();
    Interface_Rtimer->setInterval(Interface_rt);//毫秒
    Timer_10s->stop();
    Timer_10s->setInterval(Timer_10s_rt);//毫秒
    Interface_Rtimer->start();
    Timer_10s->start();
    rest_par_1=0;
    rest_par_2=0;
    rest_par_3=0;
    rest_par_4=0;
    CAN_send_control_0=0;
    CAN_send_control_1=0;
    CAN_send_control_2=0;
    CAN_send_control_3=0;
    MA_ERR_Reset_k=0;
    Save_Data=0;
    fileName="";
    SetRunTime_BIT=0;
    SetRunTime=0;
    Belord_PTC_9000::rest_data_info();
    Belord_PTC_9000::rest_data_ma();

}




void MainWindow::Equ_runinfo_init()    //设备运行数据初始化
{

    ui->tableView_eq01->setModel(&Belord_PTC_9000_mode_info);
}

void MainWindow::Hid_fault_Init()
{


}


void MainWindow::Data_Refresh()            //数据刷新函数
{
    Belord_PTC_9000::EQ_runinfo();
    ui->tableView_eq01->viewport()->update();

}


void MainWindow::Protect_fun()
{

}

void MainWindow::HidFault_Scan()           //故障扫描
{

}


void MainWindow::Interface_Refresh()       //界面刷新函数
{
    emit Displayrefresh_signal(1);
    NowTime = QTime::currentTime();
    int t = StartTime.msecsTo(NowTime)/1000;
    QDateTime curDataTime=QDateTime::currentDateTime();

    ui->label_DateTime->setText(curDataTime.toString("yyyy-MM-dd hh:mm:ss"));
    ui->LCDHour->display((int) t/360000);
    ui->LCDMin->display((int) (t/60)%60);
    ui->LCDSec->display(t%60);
    ui->label_SetRunTime->setText(QString::number(SetRunTime));
    if(SetRunTime_BIT==1)
    {
        NowTime_SetRun= QTime::currentTime();
        int t_run=StartTime_SetRun.msecsTo(NowTime_SetRun)/1000;
        ui->LCDMin_SetRunTime->display(QString::number(SetRunTime-t_run));
        qDebug()<<"t_run="<<t_run;
        ui->label_SetRunTime->setText(QString::number(SetRunTime));
        if(SetRunTime-t_run==0)
        {
            if(Belord_PTC_9000_ma.BMS_SET== 0x1||Belord_PTC_9000_ma.BMS_SET== 0x2||Belord_PTC_9000_ma.BMS_SET== 0x3)
            {
                Belord_PTC_9000_ma.BMS_SET=0x00;
                ui->radioButton_SetMode_cooling->setChecked(0);
                ui->radioButton_SetMode_heating->setChecked(0);
                ui->radioButton_SetMode_off->setChecked(1);
                SetRunTime_BIT=0;
                ui->pushButton_Timing_Starts->setText("开始");
            }
            StartTime_SetRun.isNull();
            qDebug()<<"t_run="<<t_run;
        }

    }
    else
    {
        ui->LCDMin_SetRunTime->display(QString::number(0));

    }



    ui->label_show_BMS_TEMP_Target->setText(QString::number(Belord_PTC_9000_ma.TEMP_Target)+"℃");
    ui->label_show_BMS_HV->setText(QString::number(Belord_PTC_9000_ma.BMS_HV)+"V");


    if(Belord_PTC_9000_ma.MA_ERR_Reset==1)
    {
        MA_ERR_Reset_k++;
        if(MA_ERR_Reset_k>2*(1000/Interface_rt))
        {
            Belord_PTC_9000_ma.MA_ERR_Reset=0;
            MA_ERR_Reset_k=0;
        }
    }

    qDebug()<<Belord_PTC_9000_ma.MA_ERR_Reset<<" | "<<MA_ERR_Reset_k;
    switch(Belord_PTC_9000_info.TMS_WORK)
    {
    case 0:
        ui->label_TMS_WORK->setText("关机模式");
        break;
    case 1:
        ui->label_TMS_WORK->setText("制冷模式");
        break;
    case 2:
        ui->label_TMS_WORK->setText("制热模式");
        break;
    case 3:
        ui->label_TMS_WORK->setText("自循环模式");
        break;

    }



    switch(Belord_PTC_9000_info.TMS_DCH)
    {
    case 0:
        ui->label_TMS_DCH->setText("断开状态");
        break;
    case 1:
        ui->label_TMS_DCH->setText("闭合状态");
        break;

    }




    switch(Belord_PTC_9000_info.WATER_MODE)
    {
    case 0:
        ui->label_WATER_MODE->setText("关闭");
        break;
    case 1:
        ui->label_WATER_MODE->setText("开启");
        break;

    }
    ui->label_OUT_TEMP->setText(QString::number(Belord_PTC_9000_info.OUT_TEMP)+"℃");
    ui->label_IN_TEMP->setText(QString::number(Belord_PTC_9000_info.IN_TEMP)+"℃");
    ui->label_TMS_KW->setText(QString::number(Belord_PTC_9000_info.TMS_KW)+"KW");
    ui->label_Fault_Data->setText(QString::number(Belord_PTC_9000_info.Fault_Data));
    ui->label_Fault_Level->setText(QString::number(Belord_PTC_9000_info.Fault_Level));
    ui->label_CAN_OUT1->setText(QString::number(Belord_PTC_9000_ma.CAN_OUT1)+"%");
    ui->label_CAN_OUT2->setText(QString::number(Belord_PTC_9000_ma.CAN_OUT2)+"%");
    ui->label_CAN_OUT3->setText(QString::number(Belord_PTC_9000_ma.CAN_OUT3)+"%");
    ui->label_CAN_OUT4->setText(QString::number(Belord_PTC_9000_ma.CAN_OUT4)+"%");

}
void MainWindow::Fault_show()
{
    FaultList.clear();
    Belord_PTC_9000::Fault_processing();
    FaultList.append(Belord_PTC_9000_info.FaultList);
    FaultListModel->setStringList(FaultList);
    ui->listView_fault_list->viewport()->update();//故障列表刷新
}

void MainWindow::EQRun_loglist()
{


}


void MainWindow::save_data()
{


}

void MainWindow::Auto_Runlog_info()
{


}

void MainWindow::ADD_Savedata_Titlebar()
{


}

void MainWindow::ADD_Savedata_Line()
{


}



void MainWindow::on_checkBox_mancotr_stateChanged(int arg1)
{
    if(arg1==2)
    {
        CAN_send_control_0=1;
    }
    else
    {
        CAN_send_control_0=0;

    }
}


void MainWindow::on_actionCAN_triggered()
{
    canset->show();
}


void MainWindow::on_actionactionMQTT_triggered()
{

}


void MainWindow::on_pushButton_time_clear_clicked()
{
    StartTime=QTime::currentTime();
}


void MainWindow::on_pushButton_faultclear_clicked()
{
    Belord_PTC_9000_info.Fault_Data=0;
    Belord_PTC_9000_info.PTCHighVoltageOvervoltage=0;
    Belord_PTC_9000_info.PTCHighVoltageUndervoltage=0;
    Belord_PTC_9000_info.IGBTFault=0;
    Belord_PTC_9000_info.PTCOvercurrent=0;
    Belord_PTC_9000_info.WaterTemperatureSensorFault=0;
    Belord_PTC_9000_info.IGBTTemperatureSensorFault=0;
    Belord_PTC_9000_info.OutletTemperatureTooHigh=0;
    Belord_PTC_9000_info.IGBTTemperatureTooHigh=0;
    Belord_PTC_9000_info.CANCommunicationFault=0;
    Belord_PTC_9000_info.PTCHeatingElementShortCircuit=0;
    Belord_PTC_9000_info.PTCHeatingElementOpenCircuit=0;
    Belord_PTC_9000_info.WaterPumpFeedback=0;
    Belord_PTC_9000_info.FaultList.clear();
    FaultList.clear();
}


void MainWindow::on_pushButton_Timing_Starts_clicked()
{


    if((ui->radioButton_SetMode_cooling->isChecked()==1 || ui->radioButton_SetMode_heating->isChecked()==1 || ui->radioButton_SetMode_self_cycle->isChecked()==1)&& SetRunTime>0)
    {
        if(SetRunTime_BIT==0)
        {
            SetRunTime_BIT=1;
            StartTime_SetRun=QTime::currentTime();
            ui->pushButton_Timing_Starts->setText("取消");

        }
        else
        {
            SetRunTime_BIT=0;

            ui->pushButton_Timing_Starts->setText("开始");
        }
    }

    else

    {
        QMessageBox::information(this, QStringLiteral("提示"), QStringLiteral("需设备为运行状态或定时时长大于0"));
    }
}


void MainWindow::on_pushButton_Timing_reset_clicked()
{
    StartTime_SetRun=QTime::currentTime();
}


void MainWindow::on_pushButton_SetTiming_clicked()
{
    SetRunTime=ui->spinBox_SetRunTime->value();
}




void MainWindow::on_radioButton_SetMode_off_clicked(bool checked)
{
    qDebug()<<checked;
    if(checked==1)
    {
        Belord_PTC_9000_ma.BMS_SET=0;
    }
}


void MainWindow::on_radioButton_SetMode_cooling_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.BMS_SET=1;
    }
}


void MainWindow::on_radioButton_SetMode_heating_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.BMS_SET=2;
    }
}


void MainWindow::on_radioButton_SetMode_self_cycle_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.BMS_SET=3;
    }
}


void MainWindow::on_radioButton_BMS_DCH_OFF_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.BMS_DCH=0;
    }
}


void MainWindow::on_radioButton_BMS_DCH_ON_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.BMS_DCH=1;
    }

}


void MainWindow::on_radioButton_BMS_UP_HV_UP_clicked(bool checked)
{    if(checked==1)
    {
        Belord_PTC_9000_ma.UP_HV=0;
    }

}


void MainWindow::on_radioButton_BMS_UP_HV_DOWN_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.UP_HV=1;
    }
}


void MainWindow::on_radioButton_BMS_Charge_State_NO_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.Charge_State=0;
    }
}


void MainWindow::on_radioButton_BMS_Charge_State_YES_clicked(bool checked)
{
    if(checked==1)
    {
        Belord_PTC_9000_ma.Charge_State=1;
    }
}


void MainWindow::on_pushButton_BMS_HV_clicked()
{
    Belord_PTC_9000_ma.BMS_HV=ui->spinBox_BMS_HV->value();
}


void MainWindow::on_pushButton_BMS_TEMP_Target_clicked()
{
    Belord_PTC_9000_ma.TEMP_Target=ui->spinBox_BMS_TEMP_Target->value();
}


void MainWindow::on_checkBox_Debug_stateChanged(int arg1)
{
    if(arg1==2)
    {
        CAN_send_control_1=1;
    }
    else
    {
        CAN_send_control_1=0;

    }
}


void MainWindow::on_checkBox_PC_CONTROL_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.PC_CONTROL=1;
    }
    else
    {
        Belord_PTC_9000_ma.PC_CONTROL=0;

    }
}


void MainWindow::on_checkBox_CAN_BIT_OUT1_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT1=1;
    }
    else
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT1=0;

    }
}


void MainWindow::on_pushButton_CAN_OUT1_clicked()
{
    Belord_PTC_9000_ma.CAN_OUT1=ui->spinBox_CAN_OUT1->value();
}
void MainWindow::on_checkBox_CAN_BIT_OUT2_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT2=1;
    }
    else
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT2=0;

    }
}


void MainWindow::on_pushButton_CAN_OUT2_clicked()
{
    Belord_PTC_9000_ma.CAN_OUT2=ui->spinBox_CAN_OUT2->value();
}
void MainWindow::on_checkBox_CAN_BIT_OUT3_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT3=1;
    }
    else
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT3=0;

    }
}


void MainWindow::on_pushButton_CAN_OUT3_clicked()
{
    Belord_PTC_9000_ma.CAN_OUT3=ui->spinBox_CAN_OUT3->value();
}

void MainWindow::on_checkBox_CAN_BIT_OUT4_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT4=1;
    }
    else
    {
        Belord_PTC_9000_ma.CAN_BIT_OUT4=0;

    }
}


void MainWindow::on_pushButton_CAN_OUT4_clicked()
{
    Belord_PTC_9000_ma.CAN_OUT4=ui->spinBox_CAN_OUT4->value();
}




void MainWindow::on_checkBox_CAN_BIT_CTRL_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.CAN_BIT_CTRL=1;
    }
    else
    {
        Belord_PTC_9000_ma.CAN_BIT_CTRL=0;

    }
}


void MainWindow::on_pushButton_CAN_CTRL_clicked()
{
    if(Belord_PTC_9000_ma.CAN_CTRL==1)
    {
        Belord_PTC_9000_ma.CAN_CTRL=0;
        ui->pushButton_CAN_CTRL->setText("开");
    }
    else
    {
        Belord_PTC_9000_ma.CAN_CTRL=1;
        ui->pushButton_CAN_CTRL->setText("关");
    }

}


void MainWindow::on_checkBox_MA_BIT_ERR_Reset_stateChanged(int arg1)
{
    if(arg1==2)
    {
        Belord_PTC_9000_ma.MA_ERR_Reset=1;
    }
    else
    {
        Belord_PTC_9000_ma.MA_ERR_Reset=0;

    }
}


void MainWindow::on_pushButton_MA_ERR_Reset_clicked()
{

        Belord_PTC_9000_ma.MA_ERR_Reset=1;

}


void MainWindow::on_pushButton_password_clicked()
{
    QString str = ui->lineEdit_password->text();
    quint32 password_box_0,password_box_1,password_box_2,password_box_3;
    if(str.length()==4)
    {
        password_box_0=str.at(3).toLatin1();
        qDebug()<<"password_box_0:"<<password_box_0;
        password_box_1=str.at(2).toLatin1();
        password_box_2=str.at(1).toLatin1();
        password_box_3=str.at(0).toLatin1();
        Belord_PTC_9000_ma.Control_Password=password_box_0|(password_box_1<<8)|(password_box_2<<16)|(password_box_3<<24);;

    }
    else
    {
        Belord_PTC_9000_ma.Control_Password=0;
    }
}

